import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:uuid/uuid.dart';
import 'package:ymt/ui/download_page.dart';
import 'package:ymt/utils/nav.dart';


class SettingPage extends StatelessWidget {

  final BuildContext pageContext;
  final String _loginToken;

  SettingPage(this.pageContext, this._loginToken);

  @override
  Widget build(BuildContext context) {
    List<Widget> buttons = List();
    buttons.add(buildClickItem(buildButtonColumn(Icons.cloud_download,"下载"), (){
      Nav.wrapNav(pageContext, DownloadPage());
    }));
    buttons.add(buildClickItem(buildButtonColumn(Icons.exit_to_app,"退出登录"), (){
      logout();
    }));
    return _buildBackStack(Center(child: Row(children: buttons, mainAxisAlignment: MainAxisAlignment.center,),));
  }

  Widget _buildBackStack(content){
    var back = click(Container(
      child: Icon(Icons.arrow_back_ios),
      margin: EdgeInsets.only(left: 15, top: 30),
      padding: EdgeInsets.all(10),
      decoration: BoxDecoration(shape: BoxShape.circle,
          border: Border.all(color: Colors.white,)),), ()=>Navigator.pop(pageContext));
    return Stack(children: <Widget>[content, back],alignment: AlignmentDirectional.topStart,);
  }

  Future<void> logout() async {
    Dio dio = Dio();
    dio.options.baseUrl = "http://nkjjq.cn/api/v1";
    dio.options.responseType = ResponseType.plain;
    dio.interceptors.add(LogInterceptor(responseHeader:false, responseBody: true));
    var uuid = Uuid().v5(Uuid.NAMESPACE_URL, "ymt");
    print(uuid);
    Response response = await dio.get("/logout_tv.json", queryParameters: {"installation_id": uuid,"login_token":_loginToken,});
    print(response.data);
    Navigator.pop(pageContext, "logout");
  }

  buildClickItem(child, onTap) {
    return click(
        Container(child: child, width: 90, height: 90, margin: EdgeInsets.only(left: 20, right: 20),
          decoration: BoxDecoration(shape: BoxShape.circle, border: Border.all(color: Colors.white)),),
        onTap);
  }
  
  Column buildButtonColumn(IconData icon, String label) {
    Color color = Colors.white;
    return new Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        new Icon(icon, color: color),
        new Container(
          margin: const EdgeInsets.only(top: 8.0),
          child: new Text(
            label,
            style: new TextStyle(
              fontSize: 12.0,
              fontWeight: FontWeight.w400,
              color: color,
            ),
          ),
        ),
      ],
    );
  }

  Widget click(child, onTap) {
    return GestureDetector(child: child, onTap: onTap,);
  }

}